/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

/*
 * Utility class that correctly loads the JAWT dll
 * Contributed by Michele Puccini
 */
package scenic.jni;

import java.security.AccessController;
import java.security.PrivilegedAction;

public class JAWTLoader
{
	private static boolean loadedAWTImpl = false;
	
	public static void loadAWTImpl()
	{
		if (!loadedAWTImpl)
		{
			synchronized (JAWTLoader.class)
			{
				if (!loadedAWTImpl)
				{
					AccessController.doPrivileged(new PrivilegedAction<Object>()
					{
						public Object run()
						{
							boolean isOSX = System.getProperty("os.name").equals("Mac OS X");
							if (!isOSX)
							{
								// Must pre-load JAWT on all non-Mac platforms to
								// ensure references from jogl_awt shared object
								// will succeed since JAWT shared object isn't in
								// default library path
								try
								{
									System.loadLibrary("jawt");
								}
								catch (UnsatisfiedLinkError e)
								{
									// Accessibility technologies load JAWT themselves; safe to continue
									// as long as JAWT is loaded by any loader
									if (e.getMessage().indexOf("already loaded") == -1)
									{
										throw e;
									}
								}
							}
							
							return null;
						}
					});
					loadedAWTImpl = true;
				}
			}
		}
	}
}
